package net.bmwgroup.easyrule.demo.facts;

import net.bmwgroup.easyrule.demo.actions.ActionExecutor;
import net.bmwgroup.easyrule.demo.actions.ActionFactory;
import net.bmwgroup.easyrule.demo.facts.entity.Address;
import net.bmwgroup.easyrule.demo.facts.entity.Location;
import net.bmwgroup.easyrule.demo.facts.entity.User;
import net.bmwgroup.easyrule.demo.facts.entity.Vehicle;
import org.jeasy.rules.api.Facts;

public class FactsInitUtils {

    public static Facts initFacts() {
        Facts facts = new Facts();

        //init facts
        BaseDimension<Double> temperatureFact = new BaseDimension<>("Temperature", "Temperature", "Temperature", 44D);
        BaseDimension<Double> fuelFact = new BaseDimension<>("fuelFact", "fuelFact", "fuelFact", 20D);
        BaseDimension<Double> mileageFact = new BaseDimension<>("Mileage", "Mileage", "Mileage", 1000D);
        BaseDimension<Double> speedFact = new BaseDimension<>("Speed", "Speed", "Speed", 120D);
        BaseDimension<String> weatherFact = new BaseDimension<>("Weather", "Weather", "Weather", "Raining");
        Location location = new Location();
        location.setLon(1123D);
        location.setLat(32D);

        BaseDimension<Location> locationFact = new BaseDimension<>("Location", "Location", "Location", location);
        BaseDimension<String> holidayFact = new BaseDimension<>("Holiday", "Holiday", "Holiday", "May Day");

        facts.put("temperature", temperatureFact);
        facts.put("fuel", fuelFact);
        facts.put("mileage", mileageFact);
        facts.put("speed", speedFact);
        facts.put("weather", weatherFact);

        for (int i = 0; i < 100; i++) {
            User user = createUser("userName" + i);
            Address address = createAddress(String.valueOf(i));
            Vehicle vehicle = createVehicle(String.valueOf(i));
            BaseDimension<User> userFact = new BaseDimension<>("User", "User", "User", user);
            BaseDimension<Address> addressFact = new BaseDimension<>("Address", "Address", "Address", address);
            BaseDimension<Vehicle> vehicleFact = new BaseDimension<>("Vehicle", "Vehicle", "Vehicle", vehicle);
            facts.put("user" + i, userFact);
            facts.put("address" + i, addressFact);
            facts.put("vehicle" + i, vehicleFact);

        }


        facts.put("location", locationFact);
        facts.put("holiday", holidayFact);

        //init actions
        ActionFactory actionFactory = new ActionFactory(facts);
        ActionExecutor actionExecutor = new ActionExecutor(facts);
        facts.put("actionFactory", actionFactory);
        facts.put("actionExecutor", actionExecutor);
        return facts;
    }

    private static User createUser(String userName) {
        User user = new User();
        Address address = new Address();
        address.setProvinceName("jiangsu");
        address.setCityName("nanjing");
        address.setCountyName("jiangning");
        user.setAddress(address);
        user.setAge(34);
        user.setGender("male");
        user.setName(userName);
        user.setJob("Software engineer");
        return user;
    }

    private static Address createAddress(String addressName) {
        Address address = new Address();
        address.setProvinceName("jiangsu");
        address.setCityName("nanjing");
        address.setCountyName("jiangning");
        address.setAddressName(addressName);
        return address;
    }

    private static Vehicle createVehicle(String type) {
        Vehicle vehicle = new Vehicle();
        vehicle.setColor("red");
        vehicle.setType("X" + type);
        vehicle.setPrice(560000D);
        return vehicle;
    }
}
